package com.laojin.lemonpdf.controller;


import com.laojin.lemonpdf.entity.Student;
import com.laojin.lemonpdf.entity.Teacher;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.data.JRMapArrayDataSource;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*********************************************************
 * 文件名称：JaspereReportController.java
 * 系统名称：lemon-pdf pdf导出处理工具
 * 模块名称：com.laojin.lemonpdf.controller
 * 功能说明：
 * 开发人员：jinyh
 * 开发时间：2020/4/2 15:25
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
@Controller
@RequestMapping("/jrtest")
public class JaspereReportController {

    @RequestMapping("/getjr")
    public void test(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.读取.japser文件，构建输入流
        String path = ClassUtils.getDefaultClassLoader().getResource("static/pdftemp").getPath()+ "/Blank_A4.jasper";
       // String rootPath = Thread.currentThread().getClass().getResource("/").getPath();
        //InputStream in = request.getServletContext().getResourceAsStream(path);
        Map<String, Object> map = new HashMap<String, Object>();
        //2.创建print
        JasperPrint print = JasperFillManager.fillReport(path,map,new JREmptyDataSource());
        //3.使用Exporter导出PDF
        JasperExportManager.exportReportToPdfStream(print,response.getOutputStream());
    }
    @RequestMapping("/getjrsubtest")
    public void getjrsubtest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.读取.japser文件，构建输入流
        String path = ClassUtils.getDefaultClassLoader().getResource("static/pdftemp").getPath()+ "/main.jasper";
        // String rootPath = Thread.currentThread().getClass().getResource("/").getPath();
        //InputStream in = request.getServletContext().getResourceAsStream(path);
        Map<String, Object> map = new HashMap<String, Object>();
        //2.创建print
        JasperPrint print = JasperFillManager.fillReport(path,map,new JREmptyDataSource());
        //3.使用Exporter导出PDF
        JasperExportManager.exportReportToPdfStream(print,response.getOutputStream());
    }
    @RequestMapping("/getjrmap")
    public void getjrmap(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.读取.japser文件，构建输入流
        String path = ClassUtils.getDefaultClassLoader().getResource("static/pdftemp").getPath()+ "/parameters.jasper";
        File file = new File(path);
        InputStream in = new FileInputStream(file);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name","老金");
        map.put("sex","男");
        List<Map<String, ?>> list = new ArrayList<Map<String, ?>>();
        list.add(map);
        list.add(map);
        list.add(map);
        list.add(map);
        JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(list);
       //JRBeanCollectionDataSource
        //2.创建print
        //JasperPrint print = JasperFillManager.fillReport(in,map,new JREmptyDataSource());
        JasperPrint print = JasperFillManager.fillReport(in,map,dataSource);
        //JasperPrint print = JasperFillManager.fillReport(in,map); //导出空白
       //JasperPrint print = JasperFillManager.fillReport(path,map); //导出空白
        //3.使用Exporter导出PDF
        JasperExportManager.exportReportToPdfStream(print,response.getOutputStream());
    }
    @RequestMapping("/getjrbean")
    public void getjrbean(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.读取.japser文件，构建输入流
        String path = ClassUtils.getDefaultClassLoader().getResource("static/pdftemp").getPath()+ "/javabeantest.jasper";
        File file = new File(path);
        InputStream in = new FileInputStream(file);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name","老金");
        map.put("sex","男");
        List<Map<String, ?>> list = new ArrayList<Map<String, ?>>();
        for (int i = 0; i < 60; i++) {
            list.add(map);
        }
        JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(list);
        //2.创建print
        //JasperPrint print = JasperFillManager.fillReport(in,map,new JREmptyDataSource());
        JasperPrint print = JasperFillManager.fillReport(in,map,dataSource);
        //3.使用Exporter导出PDF
        JasperExportManager.exportReportToPdfStream(print,response.getOutputStream());
    }
    @RequestMapping("/getjrsub")
    public void getjrsub(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.读取.japser文件，构建输入流
        String path = ClassUtils.getDefaultClassLoader().getResource("static/pdftemp").getPath()+ "/testmain.jasper";
        String studentPath = ClassUtils.getDefaultClassLoader().getResource("static/pdftemp").getPath()+ "/student.jasper";
        String teacherPath = ClassUtils.getDefaultClassLoader().getResource("static/pdftemp").getPath()+ "/teacher.jasper";
        File file = new File(path);
        InputStream in = new FileInputStream(file);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("studentpath",studentPath);
        map.put("teacherpath",teacherPath);
        Map<String, Object> map1 = new HashMap<>();
        List<Student> studentList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            studentList.add(new Student("小张"+i,i%2==0?"男":"女",i+10,"年级"+i));
        }
        List<Teacher> teacherList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            teacherList.add(new Teacher("小张"+i,i%2==1?"男":"女",i+10));
        }
        map.put("student",studentList);
        map.put("teacher",teacherList);
        map1.put("student",studentList);
        map1.put("teacher",teacherList);
        List<Map<String, ?>> list = new ArrayList<Map<String, ?>>();
        list.add(map1);

       // JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(list);
        //2.创建print
        //JasperPrint print = JasperFillManager.fillReport(in,map,new JREmptyDataSource());
        JasperPrint print = JasperFillManager.fillReport(in,map,new JREmptyDataSource() );
        //3.使用Exporter导出PDF
        JasperExportManager.exportReportToPdfStream(print,response.getOutputStream());
    }
}
